<?php

namespace app\api\model;

use fast\Random;
use think\Db;
use think\Model;


class Index extends Model
{
	
	
	//获取展示ID
	public function getBio()
	{
		$num = Random::numeric('10');
		$res = Db::name('user')->where('bio', $num)->find();
		if ($res) {
			$this->getBio();
		}
		return $num;
	}
	
	/**
	 * Notes:验证身份证号
	 * Auther: gx<1187593079@qq.com>
	 * Date: 2023/10/24/024
	 * Time: 13:46
	 * @param $idCard
	 * @return bool
	 */
	function validateIDCard($idCard)
	{
		// 去除空格并转换为大写
		$idCard = strtoupper(trim($idCard));
		
		// 正则表达式验证身份证号码格式
		$pattern = '/^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}(\d|X|x)$/';
		if (!preg_match($pattern, $idCard)) {
			return false;
		}
		
		// 校验身份证号码最后一位是否正确
		$idLength    = strlen($idCard);
		$weightedSum = 0;
		$factor      = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
		$validCode   = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
		for ($i = 0; $i < $idLength - 1; $i++) {
			$weightedSum += intval($idCard[$i]) * $factor[$i];
		}
		$mod = $weightedSum % 11;
		if ($validCode[$mod] != $idCard[17]) {
			return false;
		}
		
		return true;
	}
	
	
	public function cdnData($data)
	{
		foreach ($data as $k => $v) {
			if (isset($v['avatar']) && $v['avatar']) {
				$v['avatar'] = cdnurl($v['avatar'],true);
			}
			if (isset($v['image']) && $v['image']) {
				$v['image'] = cdnurl($v['image'],true);
			}
			if (isset($v['images']) && $v['images']) {
				$images = explode(',', $v['images']);
				foreach ($images as $key => $value) {
					if ($value) {
						$value        = cdnurl($value, true);
						$images[$key] = $value;
					}
				}
				$v['images'] = $images;
			}
			if (isset($v['video']) && $v['video']) {
				$v['video'] = cdnurl($v['video'],true);
			}
			
			//一维数组
			if($k==='image' && $data['image']){
				$v=cdnurl($v,true);
			}
			if($k==='images'&& $data['images']){
				$images=explode(',',$v);
				foreach ($images as $key => $value) {
					if ($value) {
						$value        = cdnurl($value, true);
						$images[$key] = $value;
					}
				}
				$v=$images;
			}
			if($k==='video' && $data['video']){
				$v=cdnurl($v,true);
			}
			if($k==='avatar' && $data['avatar']){
				$v=cdnurl($v,true);
			}
			$data[$k] = $v;
		}
		return $data;
	}
	

}
